package com.ftg.learn.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ftg.learn.entity.LearnStudentEntity;
import com.ftg.learn.mapper.LearnStuRedisMapper;
import com.ftg.learn.service.LearnStuRedisService;
import com.ftg.learn.vo.StudentListVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 布衣
 */
@Service
@Slf4j
public class LearnStuRedisServiceImpl implements LearnStuRedisService {

    @Resource
    private LearnStuRedisMapper learnStuRedisMapper;

    @Resource
    @Qualifier("masterRedisTemplate")
    RedisTemplate<String, String> masterRedisTemplate;


    RedisTemplate<String, String> slaveRedisTemplate;

    @Resource
    private ApplicationContext applicationContext;

    @Resource
    private ObjectMapper objectMapper;

    //可重入锁
    Lock lock = new ReentrantLock();

    @Resource
    RabbitTemplate rabbitTemplate;

    @Override
    public List<StudentListVo> list() {
        try {
            lock.lock();
            slaveRedisTemplate = applicationContext.getBean("slaveRedisTemplate", RedisTemplate.class);
            //先查找redis是否命中
            if (slaveRedisTemplate.hasKey("student:list")) {
                String json = slaveRedisTemplate.opsForValue().get("student:list");
                return objectMapper.readValue(json, List.class);
            } else {
                List<StudentListVo> list = learnStuRedisMapper.list();

//                masterRedisTemplate.watch("student:list");
//                masterRedisTemplate.multi();
                int time = 1000 + (int) (Math.random() * 4001);
                masterRedisTemplate.opsForValue().set("student:list", objectMapper.writeValueAsString(list), time, TimeUnit.SECONDS);
//                masterRedisTemplate.exec();

                return list;
            }
        } catch (JsonProcessingException e) {
            log.error("json转换异常");
        } finally {
            lock.unlock();
        }
        return null;
    }

    @Override
    public String add(LearnStudentEntity lse) throws JsonProcessingException {
        //改变成添加mq
        String s1 = objectMapper.writeValueAsString(lse);
        Map<String,String> map = new HashMap<>();
        map.put("operation","add");
        map.put("parameter",s1);
        String s = objectMapper.writeValueAsString(map);
        rabbitTemplate.convertAndSend("","stu_add_queue",s);
        return "添加成功";
    }


    @Override
    public String modify(LearnStudentEntity lse) throws JsonProcessingException {
        //改变成添加mq
        String s1 = objectMapper.writeValueAsString(lse);
        Map<String,String> map = new HashMap<>();
        map.put("operation","modify");
        map.put("parameter",s1);
        String s = objectMapper.writeValueAsString(map);
        rabbitTemplate.convertAndSend("","stu_add_queue",s);
        return "添加成功";
    }
}
